<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/1/16
 * Time: 16:31
 */
namespace app\api\controller;

use app\api\model\MtCouponInfo;
use app\util\ReturnCode;

/**
 * 优惠券
 * Class Conpon
 * @package app\api\controller
 */
class Conpon extends Base
{
    /**
     * 优惠券检查
     * @param $userID
     * @param $conponID
     * @return bool
     */
    public function checkConpon($userID,$conponID){
        if(!$userID || $conponID){
            return $this->error(ReturnCode::EMPTY_PARAMS,"缺少参数");
        }
        $conponInfo = (new MtCouponInfo())->where([["conponID","=",$conponID],["userID","=",$userID],[time(),"<","couponEnd"],["couponUseState","=","1"],["status","=","1"]])->find();
        if(!$conponInfo){
            return false;
        }
        return $conponInfo;
    }

    /**
     * 订单优惠券使用检查
     * @param $orderTotal
     * @param $couponID
     * @param $userID
     * @return bool
     */
    public function checkOrderConpon($orderTotal,$couponID,$userID){
        if(!$orderTotal || !$couponID){
            return false;
        }
        $coupon = $this->checkConpon($userID,$couponID);
        if(!$coupon){
            return false;
        }
        if($coupon['couponCondition'] < $orderTotal){
            return false;
        }
        return true;
    }
    /**
     * 用户优惠券查询
     * @return mixed
     */
    public function getUserConpon(){
        $userID = $this->request->get("userID");
        if(!$userID){
            return $this->error(ReturnCode::EMPTY_PARAMS,"缺少参数userID");
        }
        $userConponList = (new MtCouponInfo())->where([["userID","=",$userID],[time(),"<","couponEnd"],["status","=","1"]])->select();
        if(!$userConponList){
            return $this->error(ReturnCode::DB_READ_ERROR,"查询失败");
        }
        return $this->success("查询成功",$userConponList);
    }
}